export const getEChartsOption = data => {
  try {
    switch (data.type) {
      case '1': // 柱形图2D
        return getBarOption(data);
      case '2': // 柱形图3D
        return getBarOption(data);
      case '3': // 线形图
        return getLineOption(data);
      case '4': // 面积图
        return getAreaOption(data);
      case '5': // 条形图
        return getBarGraphOption(data);
      case '6': // 饼图2D
        return getPieOption(data);
      case '7': // 饼图3D
        return getPieOption(data);
      case '8': // 圆环图
        return getCircleOption(data);
      case '11': // 仪表盘
        return getGaugeOption(data);
    }
  } catch (e) {}
};

const getBarOption = data => {
  return {
    title: {
      text: ''
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    legend: {
      show: data.showLegend != '0'
    },
    grid: {
      left: '3%',
      right: '3%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: data.categories
    },
    yAxis: {
      type: 'value'
    },
    series: data.series.map(item => {
      return {
        type: 'bar',
        name: item.name,
        data: item.data
      };
    })
  };
};

const getLineOption = data => {
  return {
    title: {
      text: ''
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    legend: {
      show: data.showLegend != '0'
    },
    grid: {
      left: '3%',
      right: '3%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: data.categories
    },
    yAxis: {
      type: 'value'
    },
    series: data.series.map(item => {
      return {
        type: 'line',
        name: item.name,
        data: item.data
      };
    })
  };
};

const getAreaOption = data => {
  return {
    title: {
      text: ''
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: '3%',
      right: '3%',
      bottom: '3%',
      containLabel: true
    },
    legend: {
      show: data.showLegend != '0'
    },
    xAxis: {
      type: 'category',
      data: data.categories
    },
    yAxis: {
      type: 'value'
    },
    series: data.series.map(item => {
      return {
        type: 'line',
        name: item.name,
        data: item.data,
        areaStyle: {}
      };
    })
  };
};

const getBarGraphOption = data => {
  return {
    title: {
      text: ''
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    legend: {
      show: data.showLegend != '0'
    },
    grid: {
      left: '3%',
      right: '3%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'value'
    },
    yAxis: {
      type: 'category',
      data: data.categories
    },
    series: data.series.map(item => {
      return {
        type: 'bar',
        name: item.name,
        data: item.data
      };
    })
  };
};

const getPieOption = data => {
  return {
    title: {
      text: ''
    },
    grid: {
      left: '3%',
      right: '3%',
      bottom: '-3%',
      containLabel: true
    },
    tooltip: {
      trigger: 'item',
      formatter: '{b} : {c} ({d}%)'
    },
    legend: {
      show: data.showLegend != '0',
      orient: 'vertical',
      left: 10
    },
    series: [
      {
        type: 'pie',
        name: data.series.name,
        data: data.series.data.map(item => {
          return { name: item[0], value: item[1] };
        })
      }
    ]
  };
};

const getCircleOption = data => {
  return {
    title: {
      text: ''
    },
    grid: {
      left: '3%',
      right: '3%',
      bottom: '-3%',
      containLabel: true
    },
    tooltip: {
      trigger: 'item',
      formatter: '{b} : {c} ({d}%)'
    },
    legend: {
      show: data.showLegend != '0',
      orient: 'vertical',
      left: 10
    },
    series: [
      {
        type: 'pie',
        name: data.series.name,
        radius: ['40%', '60%'],
        data: data.series.data.map(item => {
          return { name: item[0], value: item[1] };
        })
      }
    ]
  };
};

const getGaugeOption = data => {
  return {
    title: {
      text: ''
    },
    tooltip: {
      formatter: '{a} <br/>{c} {b}'
    },
    legend: {
      show: data.showLegend != '0'
    },
    series: [
      {
        type: 'gauge',
        data: [{ name: '', value: data.data.realval }]
      }
    ]
  };
};
